package com.engine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Observable;

import com.ui.TicketUI;


public class ClientThread extends Observable implements Runnable {
    /** For reading input from socket */
    private BufferedReader br;

    /** For writing output to socket. */
    private PrintWriter pw;

    /** Socket object representing client connection */

    private Socket socket;
    private boolean running;

    public ClientThread(Socket socket) throws IOException {
        this.socket = socket;
        running = false;
        //get I/O from socket
        try {
            br = new BufferedReader(
                     new InputStreamReader(
                                           socket.getInputStream()
                                          )
                                   );
            
            pw = new PrintWriter(socket.getOutputStream(), true);
            running = true; //set status
        }
        catch (IOException ioe) {
            throw ioe;
        }
    }
	
    /** 
     *Stops clients connection
     */

    public void stopClient()
    {
        try {
		this.socket.close();
        }catch(IOException ioe){ };
    }
    
    public void sendMessage(String msg)
    {
    	pw.println(msg);
    }
    
    public void run() {
        String msg = ""; //will hold message sent from client

	pw.println("Welcome to TicketSQ");
		

	  //start listening message from client//

        try {
                while ((msg = br.readLine()) != null && running) {
                    //provide your server's logic here//
			
                    //right now it is acting as an ECHO server//
                	TicketUI.App.recv(msg);
                    //pw.println(msg); //echo msg back to client//
                }
                running = false;
            }
            catch (IOException ioe) {
                running = false;
                TicketUI.App.disconnect();
            }
        //it's time to close the socket
        try {
            this.socket.close();
            System.out.println("Closing connection");
            
        } catch (IOException ioe) { }

        //notify the observers for cleanup etc.
        this.setChanged();              //inherit from Observable
        this.notifyObservers(this);     //inherit from Observable
    }
}
